package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.DamageListGoodsDao;
import com.atguigu.jxc.entity.DamageList;
import com.atguigu.jxc.entity.DamageListGoods;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.User;
import com.atguigu.jxc.service.DamageListGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * TODO
 *
 * @author Mr.zhao
 * @version 1.0
 * @date 2022/1/4 9:21
 */
@Service
public class DamageListGoodsServiceImpl implements DamageListGoodsService {

    @Autowired
    private DamageListGoodsDao damageListGoodsDao;



    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void save(DamageList damageList, HttpSession session,
                     List<DamageListGoods> goods) {
        User currentUser = (User) session.getAttribute("currentUser");


        Integer userId = currentUser.getUserId();
        String trueName = currentUser.getTrueName();
        damageList.setUserId(userId);
        damageList.setTrueName(trueName);


        damageListGoodsDao.save(damageList);
        Integer damageListId = damageList.getDamageListId();

        goods.forEach(damageListGoods -> damageListGoods.setDamageListId(damageListId));


        damageListGoodsDao.saveGoods(goods);


    }
}
